/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.viators.servlets;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.util.HashMap;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperRunManager;

/**
 *
 * @author elvis
 */
public class DescargarManifiesto extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

        String nliqu_codigo = request.getParameter("nlipa_codigo");

        response.setContentType("application/pdf");
        response.addHeader("Content-Disposition", "attachment");
        ServletOutputStream out = response.getOutputStream();

        String resource = "com/viators/xml/Conexion.xml";
        Reader reader   = Resources.getResourceAsReader(resource);
        SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);

        try {

            String rutaReportes = getServletContext().getRealPath("/reporte/rptnroorden.jasper");
            rutaReportes = rutaReportes.replaceAll("\\\\", "/") + "/";
            File reporte = new File(rutaReportes+"");
            FileInputStream in = new FileInputStream(reporte);
            HashMap<String, Object> parametros = new HashMap<String, Object>();
            String complejo = request.getRealPath("/img/complejo.jpg");
            parametros.put("codigo", Integer.parseInt(nliqu_codigo));
            complejo = complejo.replaceAll("\\\\", "/") + "/";
            parametros.put("complejo", complejo);
            byte[] bytes = JasperRunManager.runReportToPdf(in,parametros, sqlMap.getDataSource().getConnection() );
            out.write(bytes, 0, bytes.length);
            out.flush();
            out.close();

        } catch (SQLException ex){
            ex.printStackTrace();
            throw new ServletException(ex);
        } catch(JRException ex) {
            ex.printStackTrace();
            throw new ServletException(ex);
        } finally {
            out.close();
         //   ConnectionPool.liberarConexion(conn);
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
